package department.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import department.domain.Department;
import department.service.DepartmentService;

public class ModifyDepartmentFormServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String departmentId = request.getParameter("departmentId");
		DepartmentService service = DepartmentService.getInstance();
		String url = null;
		
		try{
			Department department = service.getDepartmentById(departmentId); 
			request.setAttribute("department", department);
			url  = "/modify_department_form.jsp";
		}catch(SQLException e){
			e.printStackTrace();
			request.setAttribute("error_message", e.getMessage());
			url = "/error.jsp";
		}
		RequestDispatcher rdp = request.getRequestDispatcher(url);
		rdp.forward(request, response);
	}

}
